//弹出框高宽
var lwhith=1100;  
var lheight=700;
$(function() {
	//出入库记录列表
    var options = {
        url: ctx + "outAndStorage/outAndStorageRecord/list",
        id : "bootstrap-table",
        modalName: "出入库记录",
        showSearch: false,
        showColumns : false,
        toolbar: "toolbar",
        showRefresh:false, 
        showToggle: false, 
        pageList: [10],    
        striped: true, 
        columns: [
  		{
            title: 'id',
            field: 'id',
            visible: false
        },
		{
		    title: "序号",
		    formatter: function (value, row, index) {
		 	    return $.table.serialNumber(index);
		    }
		},
		 {
            field: 'unitid',
            title: '库房编号',
            sortable: true,
            formatter: function(value, row, index) {
            	return $.table.tooltip(value);
            }
        },
        {
        	field: 'useId',
        	title: '工具编号',
        	sortable: true,
            formatter: function(value, row, index) {
            	return $.table.tooltip(value);
            }
        },
        {
        	field: 'name',
        	title: '工具名称',
        	sortable: true,
            formatter: function(value, row, index) {
            	return $.table.tooltip(value);
            }
        },
        {
        	field: 'optype',
        	title: '类型',
        	sortable: true,
            formatter: function (value, row, index) {
                return $.table.selectDict(outAndStorage_type, value);
            }
        },
        {
        	field: 'outOperator',
        	title: '领用人',
        	sortable: true
        },
        {
        	field: 'outTime',
        	title: '领用时间',
        	sortable: true
        },
        {
        	field: 'inOperator',
        	title: '归还人',
        	sortable: true
        },
        {
        	field: 'inTime',
        	title: '归还时间',
        	sortable: true
        },
        {
            field: 'barcode',
            title: 'RFID',
            sortable: true
        }
        ]
    };
    $.table.init(options);
    load(); //初始化折线图
    loadbzt(); //初始化饼状图
   
});

var json = {
    equipmenthisWd: [],
    equipmenthisSd: [],
    currentTime: []
};
var dom = null;
var myChart = null;
var dom1 = null;
var myChart1 = null;


function initecharts() {
    option = null;
    // 指定图表的配置项和数据
    option = {
        /*标题*/
        title: {
            textStyle: {
                //字体风格,'normal','italic','oblique'
                fontStyle: 'normal',
                //字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
                fontWeight: 'bold',
                //字体系列
                fontFamily: 'sans-serif',
                //字体大小
                fontSize: 12
            }
        },
        /*坐标轴触发*/
        tooltip: {
            trigger: 'axis'
        },
        /*线条注释*/
        legend: {
            data: ['出库', '入库']
        },
        color:["#c23531","green"],
        /*工具栏 显示 下载图片*/
        toolbox: {
            show: false,
            feature: {
                saveAsImage: {}
            }
        },
        optionToContent: function (opt) {
            let axisData = opt.xAxis[0].data; //坐标数据
            let series = opt.series; //折线图数据
            let tdHeads = '<td  style="padding: 0 10px">日期</td>'; //表头
            let tdBodys = ''; //数据
            series.forEach(function (item) {
                //组装表头
                tdHeads += '<td style="padding: 0 10px">${item.name}</td>';
            });
            let table =
                '<table border="1" style="margin-left:20px;border-collapse:collapse;font-size:20px;text-align:center"><tbody><tr>${tdHeads} </tr>';
            for (var i = 0, l = axisData.length; i < l; i++) {
                for (var j = 0; j < series.length; j++) {
                    //组装表数据
                    tdBodys += '<td>${series[j].data[i]}</td>';
                }
                table += '<tr><td style="padding: 0 10px">${axisData[i]}</td>${tdBodys}</tr>';
                tdBodys = '';
            }
            table += '</tbody></table>';
            return table;
        },

        /*坐标轴类型*/
        xAxis: {
            /*类目轴，适用于离散的类目数据，为该类型时必须通过 data 设置类目数据*/
            type: 'category',
            name: '      日期',
            nameTextStyle: {
                fontSize: 15
            },
            /*坐标轴两边留白策略*/
            boundaryGap: true,
            /*类目数据*/
            data: json.currentTime,
        },
        yAxis: [{
            nameTextStyle: {
                fontSize: 15
            },
            data: json.equipmenthisWd,
            type: 'value',
            name: '出库(数量)',
            axisLabel: {
                show:true,
                showMinLabel:true,
                showMaxLabel:true,
                formatter: '{value}'
            }
        },
            {
                nameTextStyle: {
                    fontSize: 15
                },
                data: json.equipmenthisSd,
                type: 'value',
                name: '入库(数量)',

                axisLabel: {
                    show:true,
                    showMinLabel:true,
                    showMaxLabel:true,
                    formatter: '{value}'
                }
            }
        ],
        series: [
            {
                name: '出库',
                type: 'line',
                data: json.equipmenthisWd,
                yAxisIndex: 0,
                symbol: 'none',  //取消折点圆圈
                markPoint: {
                    data: [
                        {
                            type: 'max',
                            name: '最大值'
                        },
                        {
                            type: 'min',
                            name: '最小值'
                        }
                    ]
                },
            },
            {
                name: '入库',
                type: 'line',
                data: json.equipmenthisSd,
                yAxisIndex: 1,
                symbol: 'none',  //取消折点圆圈
                lineStyle :{
                	normal  :{
                		color:"green"
                	}
                },
                markPoint: {
                    data: [
                        {
                            type: 'max',
                            name: '最大值'
                        },
                        {
                            type: 'min',
                            name: '最小值'
                        }
                    ]
                }
            }
        ]
    };
    ;
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }
}

function load() {
    var obj = {};

    obj.equipID = $("#equipID").val();
    obj.date =  $("#startTime").val();

    $.ajax({
        cache: true,
        type: "POST",
        url: ctx + "outAndStorage/outAndStorageRecord/getChartData",
        async: false,
        data: obj,
        error: function (request) {
            $.modal.alertError("系统错误");
        },
        success: function (data) {
            
            dom = document.getElementById("container");
            myChart = echarts.init(dom);
            json = {
                equipmenthisWd: [],
                equipmenthisSd: [],
                currentTime: []
            };
        
            	var date = new Date();
                var year = date.getFullYear();
                var m = date.getMonth()+1;
                var strDate = (new Date(year, m, 0)).getDate();
                
                if (m >= 1 && m <= 9) {
                    m = "0" + m;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                
            	for (var i = 1; i <= strDate; i++) {
            		var num1=0; //入库数量
            		var num2=0; //出库数量
            		var num3=0; //周
            		  if (data.length > 0) {
	            		 $.each(data, function (index, row) {
	        				 if(formatUndefine(row.equipmenthisDate).substring(0, 10) == (year+'-'+m+'-'+i)){
	        					 if(row.equipmenthisSd != null){
	        						 num1=num1+1;
	        					 }
	        					 if(row.equipmenthisWd != null){
	        						 num2=num2+1;
	        					 }
	        				 }
	                     })
            		  }
        		   json.currentTime.push(year+'-'+m+'-'+i);
                   json.equipmenthisWd.push(num2);
                   json.equipmenthisSd.push(num1);
                   
				}
            initecharts();

        }
    });
}

var json1 = {
		name: [],
		toolsNums: []
	};
function loadbzt() {
    $.ajax({
        cache: true,
        type: "POST",
        url: ctx + "toolLedger/directStorage/getChartData",
        async: false,
//        data: obj,
        error: function (request) {
            $.modal.alertError("系统错误");
        },
        success: function (data) {
        	dom1 = document.getElementById("container1");
        	myChart1 = echarts.init(dom1);
            json1 = {
            		name: [],
            		toolsNums: []
            };
        
            if (data.length > 0) { 
            
            		 $.each(data, function (index, row) {
            			 json1.name.push(row.name);
                     })
                     json1.toolsNums=data;
            }
            initecharts1();
        }
    });
}

function initecharts1() {
	option1 = {
		    tooltip: {
		        trigger: 'item',
		        formatter:"{b}: {c}件 ({d}%)",
		    },
		    legend: {
		        orient: 'vertical',
		        x: 'right',
		        data:json1.name
		    },
		    series: [{
		        name: '库存动态',
		        type: 'pie',
		        selectedMode: 'single',
		        radius: [0, '90%'],
		        label: {
		            normal: {
		                position: 'inner',
		                formatter: function(params, ticket, callback) {
		                    return params.name + "\n\n" + params.percent + "%";
		                }
		            }
		        },
		        labelLine: {
		            normal: {
		                show: false
		            }
		        },
		        color: ['#756b6b', '#00d3ff', '#e87a2c','green','blue','yellow','red','black'],
		        data: json1.toolsNums
		    }]
		};
	  if (option1 && typeof option1 === "object") {
	        myChart1.setOption(option1, true);
	    }
}

